package io.renren.sf.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.renren.common.utils.Constant;
import io.renren.common.utils.R;
import io.renren.sf.entity.CbBasisStrokesEntity;
import io.renren.sf.entity.CbBasisStrokesFormworkEntity;
import io.renren.sf.service.CbBasisStrokesFormworkService;
import io.renren.sf.service.CbBasisStrokesService;
import io.renren.sf.service.FixedInformationDicService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import java.io.File;
import java.util.Arrays;
import java.util.Date;
import java.util.List;
import java.util.Map;


/**
 * 
 *
 * @author cyl_txw
 * @email cyl_txw@gmail.com
 * @date 2021-04-26 16:00:55
 */
@RestController
@RequestMapping("sf/cbbasisstrokesformwork")
@Api(tags = "基本笔画模板")
@ApiIgnore
public class CbBasisStrokesFromworkController {
    @Autowired
    private CbBasisStrokesFormworkService cbBasisStrokesFormworkService;

    @Autowired
    private CbBasisStrokesService cbBasisStrokesService;

    @Autowired
    private FixedInformationDicService fixedInformationDicService;

    @ApiOperation("获取基本笔画模板列表")
    @GetMapping("/getJbbhmbList")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "page", value = "第几页pageIndex", paramType = "query", dataType = "String", required = true),
            @ApiImplicitParam(name = "limit", value = "每页数量pageSize", paramType = "query", dataType = "String", required = true)
    })
    public R getJbbhmbList(@RequestParam Map<String, Object> params){
        long page = Long.parseLong((String) params.get("page"));
        long limit = Long.parseLong((String) params.get("limit"));
        Page<CbBasisStrokesFormworkEntity> jbbhmbPage = new Page<>(page, limit);
        cbBasisStrokesFormworkService.page(jbbhmbPage);
        List<CbBasisStrokesFormworkEntity> records = jbbhmbPage.getRecords();
        for(CbBasisStrokesFormworkEntity record:records){
            //通过基本笔画模板id获取基本笔画list
            List<CbBasisStrokesEntity> cbBasisStrokes = cbBasisStrokesService.list(new QueryWrapper<CbBasisStrokesEntity>().eq("fid",record.getId()).orderByAsc("border"));
            record.setBasisStrokes(cbBasisStrokes);
        }
        return R.ok().put("jbbhmbPage", jbbhmbPage);
    }

    @GetMapping("/createJbbhmbZip")
    public R createJbbhmbZip(){
        try {
            String zipFile = cbBasisStrokesFormworkService.createJbbhmbZip();
            fixedInformationDicService.setOne(Constant.ZipBasisStrokes.ZIPNAME.getName() ,zipFile, new Date());
            return R.ok();
        } catch (Exception e) {
            e.printStackTrace();
            return R.error("资源包不完整，还需要添加数据");
        } finally {
            System.gc();
            System.runFinalization(); // 强制回收io流
            File zip = new File(Constant.ZipBasisStrokes.YSB.getName() + Constant.ZipBasisStrokes.ZIPNAME.getName() + ".zip");
            if(zip.exists()){
                // 删除压缩包
                zip.delete();
            }
            // 删除临时目录中下载的临时文件
            File dir = new File(Constant.ZipBasisStrokes.PATH.getName());
            if (dir.exists()) {
                if (dir.isDirectory()) {
                    String[] children = dir.list();
                    // 递归删除目录中的子目录下
                    for (int i = 0; i < children.length; i++) {
                        (new File(dir, children[i])).delete();
                    }
                }
                dir.delete();
            }
        }
    }

    @ApiOperation("获取所有基本笔画模板列表")
    @GetMapping("/getAllJbbhmbList")
    public R getAllJbbhmbList(){
        List<CbBasisStrokesFormworkEntity> list = cbBasisStrokesFormworkService.list(new QueryWrapper<CbBasisStrokesFormworkEntity>());
        return R.ok().put("jbbhmbs", list);
    }

    /**
     * 信息
     */
    @ApiOperation("获取基本笔画模板信息")
    @GetMapping("/info/{id}")
    //@RequiresPermissions("sf:cbbasisstrokes:info")
    public R info(@PathVariable("id") Long id){
        CbBasisStrokesFormworkEntity cbBasisStrokesFormwork = cbBasisStrokesFormworkService.getById(id);

        return R.ok().put("cbBasisStrokesFormwork", cbBasisStrokesFormwork);
    }

    /**
     * 保存
     */
    @ApiOperation("保存")
    @PostMapping("/save")
    //@RequiresPermissions("sf:cbbasisstrokes:save")
    public R save(@RequestBody CbBasisStrokesFormworkEntity cbBasisStrokesFormwork){
        cbBasisStrokesFormworkService.save(cbBasisStrokesFormwork);

        return R.ok();
    }

    /**
     * 修改
     */
    @ApiOperation("修改")
    @PostMapping("/update")
    //@RequiresPermissions("sf:cbbasisstrokes:update")
    public R update(@RequestBody CbBasisStrokesFormworkEntity cbBasisStrokesFormwork){
        cbBasisStrokesFormworkService.updateById(cbBasisStrokesFormwork);

        return R.ok();
    }

    /**
     * 删除
     */
    @ApiOperation("删除")
    @PostMapping("/delete")
    //@RequiresPermissions("sf:cbbasisstrokes:delete")
    public R delete(@RequestBody Long[] ids){
        cbBasisStrokesFormworkService.removeByIds(Arrays.asList(ids));
        return R.ok();
    }

}
